#include <stack>
#include <vector>
//
// Created by lenovo on 25-1-8.
//
using namespace std;

class Solution
{
public:
    static bool validateStackSequences(vector<int>& pushed, vector<int>& popped)
    {
        std::stack<int> st;
        int pushi = 0;
        int popi = 0;

        while(pushi < pushed.size())
        {
            st.push(pushed[pushi++]);

            while(!st.empty() && st.top() == popped[popi])
            {
                st.pop();
                ++popi;
            }
        }

        return st.empty();
    }
};